package com.project.poetry.service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.poetry.common.constant.ConstantValue;
import com.project.poetry.common.util.CommonUtil;
import com.project.poetry.service.entity.dto.author.AuthorDTO;
import com.project.poetry.service.entity.dto.author.AuthorDetailDTO;
import com.project.poetry.service.entity.dto.author.AuthorListDTO;
import com.project.poetry.service.entity.dto.author.AuthorListOutDTO;
import com.project.poetry.service.entity.dto.code.CodeDTO;
import com.project.poetry.service.entity.po.author.AuthorPO;
import com.project.poetry.service.entity.po.poetry.PoetryPO;
import com.project.poetry.service.mapper.AuthorMapper;
import com.project.poetry.service.service.AuthorService;
import com.project.poetry.service.service.CodeService;
import com.project.poetry.service.service.PoetryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 作者业务层实现类
 * @author mengxj
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, AuthorPO> implements AuthorService {

    @Resource
    private AuthorMapper authorMapper;

    @Resource
    private CodeService codeService;

    @Resource
    private PoetryService poetryService;

    @Override
    public IPage<AuthorListOutDTO> authorList(AuthorListDTO authorList) {
        QueryWrapper<AuthorListOutDTO> wrapper = new QueryWrapper<>();
        CommonUtil.eq(wrapper, "ta.dynasty", authorList.getDynasty());
        final String name = authorList.getAuthorName();
        if(StrUtil.isNotBlank(name)){
            wrapper.and(qw -> qw.like("ta.author", name).or().like("ta.zi", name).or().like("ta.hao", name));
        }
        Integer pageNum = authorList.getPageNum(), pageSize = authorList.getPageSize();
        return this.authorMapper.selectAuthorList(CommonUtil.getPage(pageNum, pageSize), wrapper);
    }

    @Override
    public void saveAuthor(AuthorDTO author) {
        Long id = author.getId();
        if(Objects.isNull(id)){
            List<AuthorPO> list = this.authorMapper.selectList(new QueryWrapper<AuthorPO>().lambda().eq(AuthorPO::getAuthor, author.getAuthor()));
            CommonUtil.checkThrow(CommonUtil :: isNotEmpty, list, ConstantValue.SERVICE_DATA_ERROR_STATUS_CODE, "该作者已存在！！！");
            AuthorPO po = new AuthorPO();
            BeanUtils.copyProperties(author, po);
            this.authorMapper.insert(po);
        }else{
            AuthorPO po = this.authorMapper.selectById(id);
            BeanUtils.copyProperties(author, po);
            this.authorMapper.updateById(po);
        }
    }

    @Override
    public Map<String, List<AuthorPO>> getAuthorAndDynasty(){
        List<CodeDTO> dynastyList = this.codeService.getCodesByType(30);
        List<AuthorPO> authorList = this.authorMapper.selectList(new QueryWrapper<>());
        Map<String, List<AuthorPO>> dynastyAuthor = new HashMap<>(dynastyList.size());
        Map<String, List<AuthorPO>> hasMap = authorList.stream().collect(Collectors.groupingBy(AuthorPO::getDynasty));
        dynastyList.forEach(d -> {
            String dynastyName = d.getValue();
            List<AuthorPO> value = hasMap.get(dynastyName);
            dynastyAuthor.put(dynastyName, value);
        });
        dynastyAuthor.put("all", authorList);
        return dynastyAuthor;
    }

    @Override
    public void deleteById(Long id) {
        List<PoetryPO> poetryList = this.poetryService.selectPoetryByAuthorId(id);
        CommonUtil.checkThrow(CommonUtil :: isNotEmpty, poetryList,
                ConstantValue.SERVICE_DATA_ERROR_STATUS_CODE, "该作者已存在作品 ，不能删除！！！");
        this.authorMapper.deleteById(id);
    }

    @Override
    public AuthorDetailDTO authorDetailById(Long authorId) {
        AuthorPO author = authorMapper.selectById(authorId);
        List<PoetryPO> poetryList = this.poetryService.selectPoetryByAuthorId(authorId);
        return AuthorDetailDTO.builder().poetryList(poetryList).author(author).build();
    }
}
